package cn.scw.project.goods.controller;

import cn.scw.common.constant.SysConsts;
import cn.scw.framework.aspectj.lang.annotation.Log;
import cn.scw.framework.aspectj.lang.enums.BusinessType;
import cn.scw.framework.web.controller.BaseController;
import cn.scw.framework.web.domain.AjaxResult;
import cn.scw.framework.web.page.TableDataInfo;
import cn.scw.project.goods.domain.netework.bean.GAddressAddReq;
import cn.scw.project.goods.domain.vo.GAddressVo;
import cn.scw.project.goods.service.IGAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收货地址Controller
 *
 * @author api
 * @date 2020-05-20
 */
@Api(tags = "收货地址")
@RestController
@RequestMapping(SysConsts.CONTEXT_PATH+"/goods/address")
public class GAddressController extends BaseController {
    @Autowired
    private IGAddressService gAddressService;

    @ApiOperation("查询收货地址列表")
    //@PreAuthorize("@ss.hasPermi('goods:address:list')")
    @GetMapping("/list")
    public TableDataInfo list() {
        startPage();
        List<GAddressVo> list = gAddressService.selectGAddressList();
        return getDataTable(list);
    }

    @ApiOperation("查询默认收货地址")
    //@PreAuthorize("@ss.hasPermi('goods:address:list')")
    @GetMapping("/default")
    public AjaxResult defaultAddress() {
        GAddressVo gAddressVo = gAddressService.selectDefaultAddress();
        return AjaxResult.success(gAddressVo);
    }

    @ApiOperation("获取收货地址详细信息")
    //@PreAuthorize("@ss.hasPermi('goods:address:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(gAddressService.selectGAddressVoById(id));
    }

    @ApiOperation("设置默认地址")
    @PutMapping("/default/{id}")
    public AjaxResult editDefault(@PathVariable Long id){
        return AjaxResult.success(gAddressService.updateGAddressDefault(id));
    }

    @ApiOperation("新增收货地址")
    //@PreAuthorize("@ss.hasPermi('goods:address:add')")
    @Log(title = "收货地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(GAddressAddReq req) {
        return toAjax(gAddressService.insertGAddress(req));
    }

    @ApiOperation("修改收货地址")
    //@PreAuthorize("@ss.hasPermi('goods:address:edit')")
    @Log(title = "收货地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(GAddressAddReq req) {
        return toAjax(gAddressService.updateGAddress(req));
    }

    @ApiOperation("删除收货地址")
    //@PreAuthorize("@ss.hasPermi('goods:address:remove')")
    @Log(title = "收货地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(gAddressService.deleteGAddressByIds(ids));
    }
}
